package com.haisen.controller;

import com.haisen.core.utils.Constants;
import com.haisen.dataobject.dto.ResourceDto;
import com.haisen.dataobject.entity.User;
import com.haisen.service.ResourceService;
import com.haisen.service.RankServcie;
import com.haisen.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author cjbi
 */
@Controller
@Slf4j
public class IndexController {

    @Autowired
    private ResourceService resourceService;
    @Autowired
    private UserService userService;

    @Autowired
    private RankServcie userScoreServcie;

    @GetMapping("/")
    public String index(Model model) {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
         // log.info("登录用户：{}",username);
          //根据用户查找权限,对应的是路径
        Set<String> permissions = userService.findPermissions(username);
        //根据路径查对应的权限:
        List<ResourceDto> menus = resourceService.findMenus(permissions);
        //这里通过dom来传递变量
        StringBuilder dom = new StringBuilder();
        //dom赋值,首页吗,所以Menu_root_id?
        getMenuTree(menus, Constants.MENU_ROOT_ID, dom);
        //响应给前端页面
        model.addAttribute(Constants.MENU_TREE, dom);
        //添加用户信息
        User user = userService.findByUsername(username);
        model.addAttribute("user",user);
        model.addAttribute("score",userScoreServcie.findMyselfScore(user.getNickName()));
        return "base/main";
    }

    /**
     * 测试模板
     * @param model
     * @return
     */
    @GetMapping("/content")
    public String contentWrapper(Model model) {
        return "example/main";
    }



    /**
     * 获取菜单树,生产菜单,resource权限没有设置的的,默认有权限
     * @param source
     * @param pid
     * @param dom
     * @return
     */
    private List<ResourceDto> getMenuTree(List<ResourceDto> source, Long pid, StringBuilder dom) {
        List<ResourceDto> target = getChildResourceByPid(source, pid);
        //log.info("菜单列表:{},菜单树:{}", JSON.toJSONString(source),JSON.toJSONString(target));
        target.forEach(res -> {
            dom.append("<li class='treeview'>");
            dom.append("<a href='" + res.getUrl() + "'>");
            dom.append("<i class='" + res.getIcon() + "'></i>");
            dom.append("<span>" + res.getName() + "</span>");
            if (Constants.SHARP.equals(res.getUrl())) {
                dom.append("<span class='pull-right-container'><i class='fa fa-angle-left pull-right'></i> </span>");
            }
            dom.append("</a>");
            dom.append("<ul class='treeview-menu'>");
            res.setChildren(getMenuTree(source, res.getId(), dom));
            dom.append("</ul>");
            dom.append("</li>");
        });
        return target;
    }

    /**
     * 子菜单
     * @param source
     * @param pId
     * @return
     */
    private List<ResourceDto> getChildResourceByPid(List<ResourceDto> source, Long pId) {
        List<ResourceDto> child = new ArrayList<>();
        source.forEach(res -> {
            if (pId.equals(res.getParentId())) {
                child.add(res);
            }
        });
        return child;
    }

}
